<?php

namespace Home\Model;

use Think\Model;

class SiteModel extends Model {
	
	/**
	 * 搜索列表
	 *
	 * @param string $where        	
	 * @param number $page        	
	 * @param string $order        	
	 * @param intval $num
	 *        	每页显示条数
	 * @param intval $isPage
	 *        	是否需要分页
	 * @return unknown
	 */
	public function search($where = '1', $page = 1, $order = '', $num = '', $isPage = 1) {
		$page = intval ( $page ) < 1 ? 1 : $page;
		$num = $num ? $num : C ( 'page_num' );
		
		// 读取缓存
		$cacheName = get_cache_name ( $_GET ['mobile'] . 'site_search_' . $where . $page . $order . $num );
		$result = S ( $cacheName );
		if ($result) {
			return $result;
		}
		
		$result ['list'] = $this->field ( 'id' )->where ( $where )->page ( $page, $num )->order ( $order )->select ();
		foreach ( $result ['list'] as $k => $v ) {
			$result ['list'] [$k] = $this->getInfo ( $v ['id'] );
		}
		if ($isPage == 1) {
			$result ['total_num'] = $this->where ( $where )->count ();
			$result ['page'] = page_html ( $result ['total_num'], $page, $num );
			$result ['total_page'] = ceil ( $result ['total_num'] / $num );
			if ($result ['total_page'] > $page) {
				$result ['next_page'] = $page + 1;
			} else {
				$result ['next_page'] = '';
			}
			if ($page > 1) {
				$result ['prev_page'] = $page - 1;
			} else {
				$result ['prev_page'] = '';
			}
		}
		S ( $cacheName, $result, C ( 'cache_time_short' ) );
		return $result;
	}
	
	/**
	 * 获取热门小站
	 *
	 * @param unknown $cid        	
	 * @param unknown $num        	
	 */
	public function getHotSite($cid = '', $num = 10) {
		$where = '1';
		if ($cid) {
			$where .= ' and cid=' . intval ( $cid );
		}
		$num = intval ( $num );
		$order = 'viewnum desc';
		$result = $this->search ( $where, 1, $order, $num * 4 );
		
		// 随机
		$list = $result ['list'];
		unset ( $result ['list'] );
		foreach ( array_rand ( $list, $num ) as $index ) {
			$result ['list'] [] = $list [$index];
		}
		return $result;
	}
	/**
	 * 获取推荐小站
	 *
	 * @param unknown $cid        	
	 * @param unknown $num        	
	 */
	public function getSortSite($cid = '', $num = 10) {
		$where = '1';
		if ($cid) {
			$where .= ' and cid=' . intval ( $cid );
		}
		$num = intval ( $num );
		$order = 'sort desc';
		$result = $this->search ( $where, 1, $order, $num * 2 );
		
		// 随机
		$list = $result ['list'];
		unset ( $result ['list'] );
		foreach ( array_rand ( $list, $num ) as $index ) {
			$result ['list'] [] = $list [$index];
		}
		return $result;
	}
	
	/**
	 * 获取单条信息
	 *
	 * @param unknown $id        	
	 * @return Ambigous <\Think\mixed, boolean, NULL, multitype:, mixed, unknown, string, object>
	 */
	public function getInfo($id) {
		
		// 读取缓存
		$cacheName = get_cache_name ( 'site_' . $id );
		$result = S ( $cacheName );
		if ($result) {
			return $result;
		}
		
		if (intval ( $id )) {
			$where = 'id=' . intval ( $id );
		} else {
			$where = 'url="' . $id . '"';
		}
		
		$result = $this->where ( $where )->find ();
		$category = new CategoryModel ();
		$cateInfo = $category->getInfo ( $result ['cid'] );
		$result ['url'] = $cateInfo ['url'] . ($result ['url'] ? $result ['url'] : $result ['id']) . '/';
		if ($result ['thumb']) {
			$result ['thumb500'] = C ( 'img_server' ) . str_replace ( '.', '_500_500.', $result ['thumb'] );
			$result ['thumb150'] = (C ( 'img_server1' ) ? C ( 'img_server1' ) : C ( 'img_server' )) . str_replace ( '.', '_150_150.', $result ['thumb'] );
			$result ['thumb'] = C ( 'img_server' ) . $result ['thumb'];
		}
		$result ['title'] = $result ['name'];
		$result ['text200'] = cut_str ( strip_tags ( $result ['intro'] ), 200, '' );
		$result ['updatedata'] = date ( 'Y-m-d', strtotime ( $result ['updatetime'] ) );
		$result ['photo'] = $this->_getPhoto ( $result ['photo'] );
		S ( $cacheName, $result, C ( 'cache_time_long' ) );
		return $result;
	}
	
	/**
	 * 获取分类下的小站
	 *
	 * @param unknown $cid        	
	 * @param number $num        	
	 * @return Ambigous <\Home\Model\unknown, string>
	 */
	public function getCategorySite($cid, $num = '') {
		$where = '1';
		if ($cid) {
			$where .= ' and cid=' . intval ( $cid );
		}
		$page = intval ( $_GET ['page'] );
		$order = 'updatetime desc';
		$num = intval ( $num );
		$result = $this->search ( $where, $page, $order, $num );
		return $result;
	}
	private function _getPhoto($html) {
		$html = strip_tags ( $html, '<img>' );
		preg_match_all ( "/src=\"([^\"]+)/isu", $html, $rs );
		$result ['itmes'] = $rs [1];
		$result ['html'] = $html;
		return $result;
	}
}